package com.czl.module_notice.viewmodel

import android.os.Bundle
import com.czl.base.base.BaseBean
import com.czl.base.base.BaseViewModel
import com.czl.base.base.MyApplication
import com.czl.base.binding.command.BindingAction
import com.czl.base.binding.command.BindingCommand
import com.czl.base.data.DataRepository
import com.czl.base.data.bean.NoticeMsgListBean
import com.czl.base.event.LiveBusCenter
import com.czl.base.event.SingleLiveEvent
import com.czl.base.extension.ApiSubscriberHelper
import com.czl.base.util.RxThreadHelper

/**
 *
 * @Description:
 * @Author: XCH
 * @CreateDate: 2021/12/10 11:28
 */
class NoticeListViewModel(application: MyApplication, model: DataRepository) :
    BaseViewModel<DataRepository>(application, model) {

    var currentPage = 0
    var isRead = "0"
    val uc = UiChangeEvent()

    class UiChangeEvent {
        val refreshListEvent: SingleLiveEvent<Void> = SingleLiveEvent()
        val loadCompleteEvent: SingleLiveEvent<NoticeMsgListBean> = SingleLiveEvent()
        val navScoreEvent: SingleLiveEvent<String> = SingleLiveEvent()
    }

    val onLoadMoreListener: BindingCommand<Void> = BindingCommand(BindingAction {
        getNoticeMsgList()
    })
    val onRefreshListener: BindingCommand<Void> = BindingCommand(BindingAction {
        currentPage = 0
        getNoticeMsgList()
    })

    private fun getNoticeMsgList() {
        model.getNoticeMsgList(
            mapOf(
                "areaNo" to model.getAreaId(),
                "pageNum" to (currentPage + 1).toString(),
                "pageSize" to "10",
                "isRead" to isRead,
            )
        )
            .compose(RxThreadHelper.rxSchedulerHelper(this))
            .subscribe(object : ApiSubscriberHelper<BaseBean<NoticeMsgListBean>>() {
                override fun onResult(t: BaseBean<NoticeMsgListBean>) {
                    if (t.code == 200) {
                        currentPage++
                        uc.loadCompleteEvent.postValue(t.data)
                    } else {
                        uc.loadCompleteEvent.postValue(null)
                    }
                }

                override fun onFailed(msg: String?) {
                    showErrorToast(msg)
                    uc.loadCompleteEvent.postValue(null)
                }

            })
    }

    fun readNotice(noticeBean: NoticeMsgListBean.Data) {
        if (noticeBean.isRead == 0) {
            model.readNotice(noticeBean.id)
                .compose(RxThreadHelper.rxSchedulerHelper(this))
                .doOnSubscribe { showLoading() }
                .subscribe(object : ApiSubscriberHelper<BaseBean<Any?>>() {
                    override fun onResult(t: BaseBean<Any?>) {
                        dismissLoading()
                        if (t.code == 200) {
                            navPage(noticeBean.jumpLink)
                            LiveBusCenter.postRefreshNoticeMsgEvent()
                        } else {
                            showSuccessToast(t.msg)
                        }
                    }

                    override fun onFailed(msg: String?) {
                        dismissLoading()
                        showErrorToast(msg)
                    }

                })
        } else {
            navPage(noticeBean.jumpLink)
        }
    }

    private fun navPage(jumpLink: String) {
        /**
         * /workOrderManage/complaintWorkOrder?id=TSJY20220310100030102913@&@/complaint/detail?work_order_no=TSJY20220310100030102913
         */
//        var jumpLink = "/workOrderManage/complaintWorkOrder?id=TSJY20220310100030102913@&@/report/detail?work_order_no=TSJY20220310100030102913"
        if (jumpLink.contains("@&@")) {
            var linkArr = jumpLink.split("@&@")
            if (linkArr.size == 2) {
                var appLink = linkArr.last()
                var routePath: String
                var paramMap = mutableMapOf<String, String>()
                if (appLink.contains("?")) {
                    var appLinkArr = appLink.split("?")
                    routePath = appLinkArr.first()
                    if (appLinkArr.size > 1) {
                        var paramStr = appLinkArr.last()
                        if (paramStr.contains("&")) {
                            var paramArr = paramStr.split("&")
                            paramArr.forEach {
                                var inn = it.split("=")
                                paramMap[inn[0]] = inn[1]
                            }
                        } else {
                            var inn = paramStr.split("=")
                            paramMap[inn[0]] = inn[1]
                        }
                    }
                } else {
                    routePath = linkArr[1]
                }
                if (paramMap.isNotEmpty()) {
                    startContainerActivity(routePath, Bundle().apply {
                        paramMap.forEach {
                            putString(
                                it.key,
                                it.value
                            )
                        }

                    })
                } else {
                    startContainerActivity(routePath)
                }
            }
        }
    }

}